Completed
Push — master ( bebc18...c39f44 )
by Sander
01:09
created

angular.controller(ꞌSettingsCtrlꞌ)   B

Complexity

Conditions 1
Paths 27

Size

Total Lines 103

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 27
nop 4
dl 0
loc 103
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* global API */
2
3
/**
4
 * Nextcloud - passman
5
 *
6
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
7
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
(function () {
26
    'use strict';
27
28
    /**
29
     * @ngdoc function
30
     * @name passmanApp.controller:MainCtrl
31
     * @description
32
     * # MainCtrl
33
     * Controller of the passmanApp
34
     */
35
    angular.module('passmanExtension')
36
        .controller('SettingsCtrl', ['$scope', '$rootScope', 'Settings', '$location', function ($scope, $rootScope, Settings, $location) {
0 ignored issues
show
Unused Code introduced by
The parameter $rootScope is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter $location is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter Settings is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
37
            $scope.settings = {
38
                nextcloud_host: '',
39
                nextcloud_username: '',
40
                nextcloud_password: '',
41
                ignoreProtocol: true,
42
                ignoreSubdomain: true,
43
                ignorePort: true,
44
                ignorePath: true,
45
                generatedPasswordLength: 12,
46
                remember_password: true,
47
                remember_vault_password: true,
48
                refreshTime: 60,
49
                disable_browser_autofill: true,
50
                debug: false
51
            };
52
            $scope.errors = [];
53
54
            API.runtime.sendMessage(API.runtime.id, {'method': 'getRuntimeSettings'}).then(function (settings) {
55
                $scope.errors = [];
56
                if (settings) {
57
                    $scope.settings = angular.copy(settings);
58
                }
59
60
                $scope.get_vaults = function () {
61
                    if (!$scope.settings.hasOwnProperty('nextcloud_host') || !$scope.settings.hasOwnProperty('nextcloud_password') || !$scope.settings.hasOwnProperty('nextcloud_username')) {
62
                        return;
63
                    }
64
                    PAPI.username = $scope.settings.nextcloud_username;
65
                    PAPI.password = $scope.settings.nextcloud_password;
66
                    PAPI.host = $scope.settings.nextcloud_host;
67
                    $scope.extension = API.runtime.getManifest().name + ' extension ' + API.runtime.getManifest().version;
68
                    PAPI.getVaults(function (vaults) {
69
                        $scope.errors = [];
70
                        var save_btn = jQuery('#save'),
71
                            login_required = jQuery('.login-req');
72
                        if (vaults.hasOwnProperty('error')) {
73
74
                            var errors = API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]);
75
                            $scope.errors.push(errors);
76
                            login_required.hide();
77
                            save_btn.hide();
78
                            $scope.$apply();
79
                            return;
80
81
                        }
82
                        login_required.show();
83
                        save_btn.show();
84
                        $scope.vaults = vaults;
85
                        $scope.$apply();
86
87
                    });
88
                };
89
90
                $scope.$watch('[settings.nextcloud_host, settings.nextcloud_username, settings.nextcloud_password]', function () {
91
                    if ($scope.settings.nextcloud_host && $scope.settings.nextcloud_username && $scope.settings.nextcloud_password) {
92
                        $scope.get_vaults();
93
                    }
94
                }, true);
95
96
                if ($scope.settings.nextcloud_host && $scope.settings.nextcloud_username && $scope.settings.nextcloud_password) {
97
                    $scope.get_vaults();
98
                }
99
                $scope.$apply();
100
            });
101
102
            $scope.saving = false;
103
            $scope.saveSettings = function () {
104
                $scope.errors = [];
105
                var settings = angular.copy($scope.settings);
106
                try {
107
                    /** global: PAPI */
108
                    PAPI.decryptString(settings.default_vault.challenge_password, settings.vault_password);
109
                } catch (e) {
110
                    $scope.errors.push(API.i18n.getMessage('invalid_vault_password'));
111
                    return;
112
                }
113
114
                $scope.saving = true;
115
                API.runtime.sendMessage(API.runtime.id, {method: "saveSettings", args: settings}).then(function () {
116
                    setTimeout(function () {
117
                        window.location = '#!/';
118
                        $scope.saving = false;
119
                    }, 750);
120
                });
121
            };
122
123
            $scope.removeSite = function (site) {
124
                var idx = $scope.settings.ignored_sites.indexOf(site);
125
                $scope.settings.ignored_sites.splice(idx, 1);
126
            };
127
128
            $scope.ignoreSite = '';
129
            $scope.addSite = function (site) {
130
                $scope.settings.ignored_sites.push(site);
131
                $scope.ignoreSite = '';
132
            };
133
134
135
            $scope.cancel = function () {
136
                window.location = '#!/';
137
            };
138
        }]);
139
}());
140
141